package com.aggron.web.servlet;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author LR2SD
 * @description
 * @date 2018-10-20 20:26
 */
@Slf4j
@WebServlet(urlPatterns = "/my/doGet",asyncSupported=true)
public class MyServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        log.info("request support async1,{}",req.isAsyncSupported());
        log.info("request start async1,{}",req.isAsyncStarted());
        //开启异步
        req.startAsync(req,resp);
        log.info("request support async2,{}",req.isAsyncSupported());
        log.info("request start async2,{}",req.isAsyncStarted());
        AsyncContext asyncContext=req.getAsyncContext();
        asyncContext.start(()->{
            try {
                ServletResponse response=asyncContext.getResponse();
                response.getWriter().print("hello,world");
                asyncContext.complete();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }
}
